#include "PlaylistMenuOLD.h"

void PlaylistMenuOLD::draw(HDC hdc)
{
	
	if(updated == false)
	{
		Bitmap bmp(width,height);
		Graphics *fromImg = Graphics::FromImage(&bmp);

		SolidBrush  brushNowPlaying(Color(255, 60, 80, 100));
		SolidBrush  brushFontColor(Color(255, 166, 200, 211));
		SolidBrush  sbrushSliderBackground(Color(255, 60, 60, 60));
		SolidBrush  brushSlider(Color(255, 85, 85, 85));
		SolidBrush  brushBackground(Color(255, 34, 34, 34));
		Pen			penBorder(Color(255, 0, 0, 0));
		Pen			penBorderSelected(Color(255, 120, 120, 120));

		FontFamily  fontFamily(L"Arial");
		Font        font(&fontFamily, 12, FontStyleRegular, UnitPixel);

		// big block
		fromImg->DrawRectangle(&penBorder, 0, 0, width-1, height-1);
		fromImg->FillRectangle(&brushBackground, 1, 1, width-2, height-2);
	
		// slider
		fromImg->DrawRectangle(&penBorder,  width-12, 0, 11, height-1);
		fromImg->FillRectangle(&sbrushSliderBackground, width-11, 1, 10, height-2);

		//song
		for(int i = 0; i < (height-4)/13 && i+top < playlist->count(); i++)
		{
	
			MusicFileData d;						
			d = playlist->getSong(i+top);

			if(i+top == playlist->getCurrentSongId()) { fromImg->FillRectangle(&brushNowPlaying, 2, 2 +(i*13), width - 4 - 11, 12); lastPlayed = playlist->getCurrentSongId(); }
			if(i+top == selected) fromImg->DrawRectangle(&penBorderSelected, 2, 2 +(i*13), width - 4 - 12, 11);


			

			if(d.length >= 36000) d.length = 0;

			WCHAR *txt1 = new WCHAR[20];
			_stprintf(txt1, _T("%d:%02d:%02d"), d.length / 60 / 60, d.length / 60 % 60, d.length % 60);
			fromImg->DrawString(txt1, -1, &font, RectF(2 + 220, (i*13), 50, 13), NULL ,&brushFontColor);
			delete[] txt1;


			WCHAR *txt = new WCHAR[260];
			_stprintf(txt, _T("%d. %s"), top+i+1 , d.name.c_str());
			fromImg->DrawString(txt, -1, &font, RectF(2, (i*13), 218, 13), NULL ,&brushFontColor);
			delete[] txt;
		}

		// slaideris

		float pp = (float)top / (playlist->count()- (height-4)/13);
		int boxTop = (height-22) * pp; 

		fromImg->FillRectangle(&brushSlider, width-11, boxTop+1, 10, 20);
		fromImg->DrawRectangle(&penBorder,  width-12, boxTop, 11, 21);



		Graphics    graphics(hdc);
		graphics.DrawImage(&bmp, x, y, width, height);

		delete fromImg;
		updated = true;
	}
	if(is_mouseDown) { calculate(); }
}

void PlaylistMenuOLD::update()
{

	if(lastPlayed >= top && lastPlayed <= top+(height-4)/13 && autoScroll == true)
	{
		if(playlist->getCurrentSongId() <= top) top = playlist->getCurrentSongId();
		if(playlist->getCurrentSongId() >= top + (height-4)/13-1) top = playlist->getCurrentSongId() - (height-4)/13 +1 ;
	}


	updated = false;

	RECT* lpRect = new RECT;

	lpRect->top = y;
	lpRect->left = x; 

	lpRect-> bottom = y+height;
	lpRect->right = x+width;


	InvalidateRect(hWnd, lpRect, false);

	delete[] lpRect;
}


int PlaylistMenuOLD::doubleClick()
{
	int r = -1; // pasirinkto punkto indeksas

	LPPOINT p = new POINT;
	GetCursorPos(p);
	ScreenToClient(hWnd, p);

	if(p->x >= x+1 && p->x <= x + width-13 &&
		p->y >= y && p->y <= y + height)
	{
		for(int i = 0; i < (height-4)/13 && i+top < playlist->count(); i++)
		{

			if(p->y >= y + 2 +(i*13) && p->y <= y + 2 +(i*13) + 12) { selected = top+i;  r = top+i;}
		}

		

	}

	/*if(p->x >= x + width-13 && p->x <= x + width &&
		p->y >= y && p->y <= y + height)
	{

		float pp = (float)(p->y - y)/height;

		top = (playlist->count()- (height-4)/13) * pp;

		if(top >= playlist->count() - (height-4)/13) top = playlist->count() - (height-4)/13;
		if(top < 0) top = 0;
	}
*/
	update();

	delete[] p;
	
	return r;
}

void PlaylistMenuOLD::scroll(int delta, bool sukti)
{
	LPPOINT p = new POINT;
	GetCursorPos(p);
	ScreenToClient(hWnd, p);

	if((p->x >= x+1 && p->x <= x + width-1 &&
		p->y >= y && p->y <= y + height) || sukti == true)
	{
		if(!sukti) top += delta;


		

		if(sukti)
		{
			selected+=delta;
			if(selected < 0) selected = 0;
			if(selected >= playlist->count()) selected = playlist->count()-1;

			if(selected < top) top = selected;
			if(selected >= top + (height-4)/13) top = selected - (height-4)/13 +1;
		}

		if(top >= playlist->count() - (height-4)/13) top = playlist->count() - (height-4)/13;
		if(top < 0) top = 0;

		//updated = false;
		autoScroll = false;
		update();
		autoScroll = true;
	}
	delete[] p;
}


void PlaylistMenuOLD::mouseDown()
{

	LPPOINT p = new POINT;
	GetCursorPos(p);
	ScreenToClient(hWnd, p);


	if(p->x >= x + width-13 && p->x <= x + width &&
		p->y >= y && p->y <= y + height)
	{
		is_mouseDown = true;

		float pp = (float)(p->y - y)/height;

		top = (playlist->count()- (height-4)/13) * pp;

		if(top >= playlist->count() - (height-4)/13) top = playlist->count() - (height-4)/13;
		if(top < 0) top = 0;
	}

	//update();

	delete[] p;

}


void PlaylistMenuOLD::mouseUp()
{
	is_mouseDown = false;
}

void PlaylistMenuOLD::calculate()
{
	LPPOINT p = new POINT;
	GetCursorPos(p);
	ScreenToClient(hWnd, p);


	if(p->y >= y+10 && p->y <= y + height-10)
	{
		float pp = (float)((p->y - y - 10))/(height-2*10);

		top = (playlist->count()- (height-4)/13) * pp;

		if(top >= playlist->count() - (height-4)/13) top = playlist->count() - (height-4)/13;
		if(top < 0) top = 0;

		/*int tmp = p->x - x - 10;
		if(lastMousePoint1 != tmp) progress = (float)tmp/(width-2*10)*100;

		lastMousePoint1 = lastMousePoint2;
		lastMousePoint2 = tmp;*/
	}
	else
	{
		
		if(p->y < y+10) top = 0;
		if(p->y > y + height-10) top = (playlist->count()- (height-4)/13);
		

		/*
		lastMousePoint1 = 0;
		lastMousePoint2 = 1;*/
	}

	lastMouse1 = lastMouse2;
	lastMouse2 = p->y;

	if(lastMouse1 != lastMouse2) update();


	delete[] p;

}

void PlaylistMenuOLD::setTop(int tt)
{
	top = tt;
	if(top >= playlist->count() - (height-4)/13) top = playlist->count() - (height-4)/13;
	if(top < 0) top = 0;
	update();
}

int PlaylistMenuOLD::getSelection()
{
	return selected;
}